/* Name: Blackhole
 * Author: Cameron Hoerig
 * Date: 5-22-12
 * Description: The Blackhole class is a random event than can occur. Upon its creation, any collisions
 * 	with wormholes temporarily disables the wormholes. If the instance is specified to have a GammaRayBurst,
 * 	the instance of GammaRayBurst is created 5 seconds before the blackhole is destroyed. Blackhole has a 
 * 	specified lifetime - after its creation, the object is destroyed once its lifetime is surpassed.
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;

public class Blackhole extends Item {
	private static LinkedList<Blackhole> idList = new LinkedList<Blackhole>();
	private Sprite sprite; // holds the sprite used for the object
	private float scale;   // scaling of the sprite
	private int SPRITE_RADIUS;
	private float rot = 0;  // initial rotation of the sprite
	private float rotSpeed = 0; // speed at which the sprite rotates
	private int lifetime;	// lifetime, in frames, of the object
	private long currentLife = 0; // time since its creation the object has existed
	private Boolean hasGamma;  // specifies if it is to have a GammaRayBurst
	private Boolean gammaCreated = false; // flags whether or not a GammaRayburst has been created
	GammaRayBurst gamma;	// GammaRayBurst pointer
	
	// Creates a black hole at the given location, with or without a gamma ray burst
	public Blackhole(int px, int py, Boolean addGamma) {
		Game.create((Item)this, 21); // Needed for update, draw, and destroy
		colType = COL_CIRCLE; // Needed for collision checking
		idList.add(this); // Needed for collision checking
		sprite = Sprite.load("/sprites/Blackhole/blackhole.png");
		SPRITE_RADIUS = sprite.getHeight()/2;
		scale = Game.randomFloat(0.8f,1.0f);
		r = (int)(SPRITE_RADIUS*scale*2.3f); // determine a random size of the object
		x = px;
		y = py;
		r = (int) (SPRITE_RADIUS*scale); // set the radius of the object
		rot = Game.randomFloat((float) (2.0f*Math.PI)); // set the initial rotation of the object
		rotSpeed = (float) (Game.randomFloat((float) (2.0f*Math.PI/400.0f), (float) (2.0f*Math.PI/200.0f)));
		hasGamma = addGamma;
		Wormhole.checkCollision(this, true);
		Planet.calculateDisFromEarth();
		switch (Game.difficulty) { // Sets lifetime based on difficulty
			case Game.DIFFICULTY_EASY:
				lifetime = 60*Game.randomInt(15, 30);
				break;
			case Game.DIFFICULTY_MEDIUM:
				lifetime = 60*Game.randomInt(25, 45);
				break;
			case Game.DIFFICULTY_HARD:
				lifetime = 60*Game.randomInt(35, 60);
				break;
		}
		Sound.effect(Sound.EFFECT_ALARM);
	}
	
	// Makes sure no current wormholes, etc collide
	public void update() {
		Wormhole.checkCollision(this, true); // check collisions with wormholes
		currentLife++;
		if(currentLife >= lifetime){ // if its lifetime is surpassed, destroy object and potential associated GammaRayBurst
			if(this.hasGamma){
				Game.delete(gamma);
			}
			Game.delete(this);
		}
		else if(hasGamma & !gammaCreated){ // if object is specified to have GammaRayBurst and 5 seconds remain until the object is destroyed,
			if(currentLife == lifetime - 10*60){ // create the instance of GammaRayBurst
				gamma = new GammaRayBurst(this.x,this.y);
				gammaCreated = true;
			}
		}
		rot += rotSpeed; // update rotation of sprite
	}
	
	// Returns whether or no the blackhole collides with the given object
	public void collision(Item other) {
		if(other instanceof Wormhole){ // if colliding with wormhole, disable wormhole
			Wormhole cOther = (Wormhole)other;
			cOther.active = false;
			cOther.setPlacementValidity(false);
		}
	}
	
	public void destroy() {
		idList.remove(this); // Needed for collision checking
		Planet.calculateDisFromEarth();
	}
	// draw the object - called to draw every new frame 
	public void draw() {
		Graphics2D gh = Game.getGraphicsHandle();
		sprite.draw(gh, x, y, rot, scale);
	}
	
	// Draws small icon in top down view, scaled the given amount
	public static void drawTdIcons(float sf) {
		Graphics2D gh = Game.getGraphicsHandle();
		for(Iterator it = idList.iterator(); it.hasNext();){
			Blackhole bid = (Blackhole)it.next();
			gh.setColor(Color.MAGENTA);
			if (bid.gammaCreated && (bid.currentLife/8)%2 == 0) {
				gh.drawOval((int)(bid.x*sf)-7, (int)(bid.y*sf)-7, 15, 15);
			} else {
				gh.fillOval((int)(bid.x*sf)-7, (int)(bid.y*sf)-7, 15, 15);
			}
		}
	}
	
	// Function needed in each item class for collision checking
	public static int checkCollision(Item other, Boolean notify) {
		int num = 0;
		for (int i = 0; i < idList.size(); i++) {
			Item idOn = (Item)idList.get(i);
			if (idOn != other) {
				if (Item.intersection(other, idOn)) {
					if (notify) {
						idOn.collision(other);
					}
					num++;
				}
			}
		}
		return num;
	}
}
